package com.supconit.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.supconit.entity.Option;
import com.supconit.entity.OptionView;
import com.supconit.service.IOptionService;
import com.supconit.service.IOptionViewService;
import com.supconit.service.IViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
    *  前端控制器
    * </p>
 *
 * @author xjh
 * @since 2019-08-17
 */
@RestController
@RequestMapping("/vote/option")
@Api(description = "")
public class OptionController extends BaseController {

    @Autowired
    private IOptionService optionService;
    @Autowired
    private IOptionViewService optionViewService;
    /**
     * 列表
     */
    @PostMapping(value = "/list")
    @ApiOperation(value = "列表", notes = "列表")
    public Object list(String type) {
        QueryWrapper<Option> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Option::getType,type);
        return optionService.list(wrapper);
    }

     /**
     * 分页
     */
    @PostMapping(value = "/page")
    @ApiOperation(value = "分页", notes = "分页")
    public Object page(@RequestParam Map<String, Object> params) {

        Page<OptionView> page = new Page<OptionView>();
        page.setCurrent(Long.valueOf(params.get("current").toString()));
        page.setSize(Long.valueOf(params.get("size").toString()));
        QueryWrapper<OptionView> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(params.get("type"))){
            wrapper.lambda().eq(OptionView::getType, params.get("type"));
        }
        if(!StringUtils.isEmpty(params.get("text"))){
            wrapper.lambda().like(OptionView::getName, params.get("text")).or().like(OptionView::getCode, params.get("text"));
        }
        return optionViewService.page(page, wrapper);
    }

//    /**
//     * 详情
//     */
//    @GetMapping(value = "/getById/{id}")
//    @ApiOperation(value = "详情", notes = "详情")
//    public Object getById(@PathVariable("id") Long id) {
//        return optionService.getById(id);
//    }

     /**
     * 新增
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "新增", notes = "新增")
    public Object save(Option option) {
        option.setId(null);
        return optionService.save(option);
    }

//    /**
//     * 删除
//     */
//    @PostMapping(value = "/removeById")
//    @ApiOperation(value = "删除", notes = "删除")
//    public Object removeById(@RequestParam Long id) {
//        return optionService.removeById(id);
//    }
//
//    /**
//     * 修改
//     */
//    @PostMapping(value = "/updateById")
//    @ApiOperation(value = "修改", notes = "修改")
//    public Object updateById(Option option) {
//        return optionService.updateById(option);
//    }
}
